var blackList = require("./blackList.js");
var userBlackList = ["登科", "咨询", "自习室", "寄宿", "考研"];

launch("com.xingin.xhs");
sleep(3000);

var 私信数组 = [
  `①吃、住、学一站式备考基地
  ②寄宿备考+择校规划+督学+模考+答疑
  ③单人间/双人间/三人间/四人间
  ⑤提供住宿宽带➕健身/游泳➕意外保险➕床上六件套`
];
var sendUsers = [];
var result = files.read("./sendUsers.txt");
if (result) {
  sendUsers = JSON.parse(result);
}

function initSearch() {
  var backReturned = id("com.xingin.xhs:id/fcn")
    .desc("返回")
    .className("android.widget.ImageView")
    .checked(false)
    .findOne(500);
  if (backReturned) {
    toastLog("已打开搜索");
  } else {
    // 打开搜索
    var searchReturned = id("com.xingin.xhs:id/hpu")
      .desc("搜索")
      .className("android.widget.Button")
      .checked(false)
      .findOne(500);
    searchReturned.click();
    sleep(500);

    // 输入内容
    var keyword = "寄宿备考";
    setText(keyword);
    sleep(1500);
    // 点击搜索按钮
    var returned = text("搜索").className("android.widget.Button").findOne(500);
    returned.click();
  }
}
initSearch();

var swipeIndex = 0;
while (true) {
  var returnedList = className("android.widget.FrameLayout")
    .clickable(true)
    .longClickable(true)
    .find();

  if (!returnedList.empty()) {
    var filterList = returnedList
      .filter((item) => {
        var noTitleErr = true;
        var noAuthorErr = true;

        // 有效数据1
        try {
          item.child(0).child(1).text();
        } catch (error) {
          noTitleErr = false;
        }
        // 有效数据2
        try {
          item.child(0).child(2).child(1).child(0).text();
        } catch (error) {
          noAuthorErr = false;
        }
        // console.log("noTitleErr: " + noTitleErr);
        // console.log("noAuthorErr: " + noAuthorErr);

        return noTitleErr && noAuthorErr;
      })
      .map((item) => {
        var title = item.child(0).child(1).text();
        var author = item.child(0).child(2).child(1).child(0).text();
        return {
          title: title,
          author: author,
          it: item,
        };
      })
      // 黑名单
      .filter((item) => {
        return !blackList.some(
          (keyword) =>
            item.title.includes(keyword) || item.author.includes(keyword)
        );
      })
      .filter((item) => {
        return !userBlackList.some((keyword) => item.author.includes(keyword));
      });

    filterList.forEach(function (item, index) {
      var newDate = new Date();
      console.log(
        newDate.getHours() +
          ":" +
          newDate.getMinutes() +
          ":" +
          newDate.getSeconds()
      );
      console.log("标题: " + item.title);
      console.log("作者: " + item.author);
      if (sendUsers.includes(item.author)) {
        console.log("已发送过私信，跳过");
        console.log();
        return;
      }

      sleep(10000);
      item.it.click();
      sendUsers.push(item.author);
      files.write("./sendUsers.txt", JSON.stringify(sendUsers), "utf-8");

      var share_button = desc("分享")
        .className("android.widget.Button")
        .clickable(true)
        .findOne(500);
      sleep(2000);
      DM_action_auther(share_button, 私信数组);

      console.log();
      sleep(3000);
      back();
      sleep(3000);
      back();
      sleep(1000 * 30);
    });
  }

  if (swipeIndex === 5) {
    console.log("滑动次数超过5次，开始向下滑动");

    for (var i = 0; i <= swipeIndex * 2; i++) {
      swipeAction_Reverse();
      sleep(500);
    }
    swipeIndex = 0;
  } else {
    swipeAction();
    swipeIndex++;
  }
}

// 私信
function DM_action_auther(share_button, 私信数组) {
  swipeAction_RTL(share_button);
  sleep(2000);
  if (
    textContains("小红书号")
      .className("android.widget.TextView")
      .clickable(true)
      .visibleToUser(true)
      .exists()
  ) {
    if (
      desc("发消息")
        .className("android.widget.ImageView")
        .clickable(true)
        .exists()
    ) {
      var returned = desc("发消息")
        .className("android.widget.ImageView")
        .clickable(true)
        .findOne(500);
      returned.click();
      sleep(2000);
      var randomIndex = Math.floor(Math.random() * 私信数组.length); // 生成一个随机索引
      var randomText = 私信数组[randomIndex];
      setText(randomText);
      sleep(2000);
      var send = text("发送")
        .className("android.widget.TextView")
        .find()
        .filter(
          (item) =>
            item && item.parent().className() == "android.widget.RelativeLayout"
        )[0];
      // send.click();
    }

    sleep(1500);
    back();
  }
}

// 向左滑动操作
function swipeAction_RTL(share_button) {
  var X1 = device.width * 0.8; // 起始点X坐标，屏幕宽度的一半
  var Y1 = share_button.bounds().centerY();

  var X2 = device.width * 0.2; // 结束点X坐标，向左滑动300像素
  var Y2 = Y1 + random(-5, +5); // 结束点Y坐标保持不变
  var duration1 = random(0.4, 0.6) * 1000; // 滑动持续时间，单位为毫秒

  // 执行滑动操作
  swipe(X1, Y1, X2, Y2, duration1);
  sleep(random(1500, 2500));
}

// 向上滑动操作
function swipeAction() {
  var X1 = device.width / 2; // 起始点X坐标，屏幕宽度的一半
  var Y1 = device.height / 1.5 + random(-5, 5); // 起始点Y坐标，屏幕高度的一半
  var X2 = X1; // 结束点X坐标，向左滑动300像素
  var Y2 = random(5, 5); // 结束点Y坐标保持不变
  var duration1 = random(0.5, 0.8) * 1000; // 滑动持续时间，单位为毫秒

  // 执行滑动操作
  swipe(X1, Y1, X2, Y2, duration1);
  sleep(1000, 2000);
}

function swipeAction_Reverse() {
  var X1 = device.width / 2 + random(-2, 2); // 起始点X坐标，屏幕宽度的一半
  var Y1 = device.height / 2 + random(-10, 10); // 起始点Y坐标，屏幕高度的一半
  var X2 = X1 + random(-5, +5); // 结束点X坐标，向左滑动300像素
  var Y2 = device.height - random(50, 100); // 结束点Y坐标保持不变
  var duration1 = random(0.5, 0.8) * 1000; // 滑动持续时间，单位为毫秒

  // 执行滑动操作
  swipe(X1, Y1, X2, Y2, duration1);
  sleep(3000);
}
